home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / stix / X.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  250 lines

  1. "======================================================================
  2. |
  3. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  4. | Written by Steve Byrne.
  5. |
  6. | This file is part of GNU Smalltalk.
  7. |
  8. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  9. | under the terms of the GNU General Public License as published by the Free
  10. | Software Foundation; either version 1, or (at your option) any later version.
  11. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  12. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. | details.
  15. | You should have received a copy of the GNU General Public License along with
  16. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  17. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  18. |
  19.  ======================================================================"
  20.  
  21.  
  22. "
  23. |     Change Log
  24. | ============================================================================
  25. | Author       Date       Change 
  26. | sbyrne     24 May 90      created.
  27. |
  28. "
  29.  
  30. Object subclass: #X
  31.     instanceVariableNames: 'socket'
  32.     classVariableNames: ''
  33.     poolDictionaries: 'XGlobals'
  34.     category: 'X hacking'
  35. !
  36.  
  37. #(BaseMask BaseId RootWindow RootWindowID BlackPixel WhitePixel VisualId)
  38.     do: [ :var | Smalltalk at: var put: nil ].
  39.  
  40.  
  41.  
  42. Behavior defineCFunc: 'connectToServer'
  43.      withSelectorArgs: 'connectToServer: hostName display: displayNum'
  44.      forClass: X class
  45.      returning: #int
  46.      args: #(string int)
  47. .
  48.  
  49. Behavior defineCFunc: 'waitForSocket'
  50.      withSelectorArgs: 'waitForSocket: socket timeOut: anInteger'
  51.      forClass: X
  52.      returning: #int    "need a boolean type"
  53.      args: #(int int)
  54. .
  55.  
  56. Behavior defineCFunc: 'byte'
  57.      withSelectorArgs: 'byteFrom: socket'
  58.      forClass: X
  59.      returning: #int
  60.      args: #(int)
  61. .
  62.  
  63. Behavior defineCFunc: 'word'
  64.      withSelectorArgs: 'wordFrom: socket'
  65.      forClass: X
  66.      returning: #int
  67.      args: #(int)
  68. .
  69.  
  70. Behavior defineCFunc: 'long'
  71.      withSelectorArgs: 'longFrom: socket'
  72.      forClass: X
  73.      returning: #int
  74.      args: #(int)
  75. .
  76.  
  77. Behavior defineCFunc: 'putByte'
  78.      withSelectorArgs: 'putByteOn: socket byte: aByte'
  79.      forClass: X
  80.      returning: #void
  81.      args: #(int int)
  82. .
  83.  
  84. Behavior defineCFunc: 'putWord'
  85.      withSelectorArgs: 'putWordOn: socket word: aWord'
  86.      forClass: X
  87.      returning: #void
  88.      args: #(int int)
  89. .
  90.  
  91. Behavior defineCFunc: 'putLong'
  92.      withSelectorArgs: 'putLongOn: socket long: aLong'
  93.      forClass: X
  94.      returning: #void
  95.      args: #(int int)
  96. !
  97.  
  98. Behavior defineCFunc: 'putBytes'
  99.      withSelectorArgs: 'putBytesOn: socket numBytes: n bytes: byteArray'
  100.      forClass: X
  101.      returning: #void
  102.      args: #(int int byteArray)
  103. !
  104.  
  105.  
  106. !X class methodsFor: 'instance creation'!
  107.  
  108. connectTo: server display: displayNum
  109.     | x |
  110.     x _ self new.
  111.     x init: (self connectToServer: server display: displayNum).
  112.     ^x
  113. !!
  114.     
  115.  
  116. !X methodsFor: 'low level protocol stream interface'!
  117.  
  118. byte
  119.     ^self byteFrom: socket
  120. !
  121.  
  122. ubyte
  123.     ^(self byteFrom: socket) bitAnd: 16rFF
  124. !
  125.  
  126. word
  127.     ^self wordFrom: socket
  128. !
  129.  
  130. uword
  131.     ^(self wordFrom: socket) bitAnd: 16rFFFF
  132. !
  133.  
  134. long
  135.     ^self longFrom: socket
  136. !
  137.  
  138. ulong
  139.     ^self longFrom: socket    "what if it's negative????"
  140. !
  141.  
  142. getString: len
  143.     | str pad |
  144.     str _ String new: len.
  145.     pad _ (4 - len) bitAnd: 3.
  146.  
  147.     1 to: len do: 
  148.     [ :i | str at: i put: (Character value: self byte) ].
  149.     pad timesRepeat: [ self byte ]. "pad to 4 byte boundary"
  150.     ^str
  151. !
  152.  
  153. getUnpaddedString: len
  154.     | str |
  155.     str _ String new: len.
  156.     1 to: len do: 
  157.     [ :i | str at: i put: (Character value: self byte) ].
  158.     ^str
  159. !
  160.  
  161. mappedId
  162.     | id |
  163.     id _ self long.
  164.     ^Registry at: id
  165.           ifAbsent: [ nil ]
  166. !
  167.  
  168. maybeMappedId: symbolArray
  169.     | id |
  170.     id _ self long.
  171.     id < symbolArray size
  172.     ifTrue: [ ^symbolArray at: id + 1 ]
  173.     ifFalse: [ ^Registry at: id
  174.                ifAbsent: [ nil ] ]
  175. !
  176.  
  177. skipBytes: len
  178.     | pad |
  179.     len timesRepeat: [ self byte ] "not terribly optimal"
  180. !
  181.  
  182. byte: aByte
  183.     self putByteOn: socket byte: aByte
  184. !
  185.  
  186. char: aChar
  187.     self putByteOn: socket byte: aChar asciiValue
  188. !
  189.  
  190. word: aWord
  191.     self putWordOn: socket word: aWord
  192. !
  193.  
  194. long: aLong
  195.     self putLongOn: socket long: aLong
  196. !
  197.  
  198. bytes: byteArray
  199.     self putBytesOn: socket numBytes: byteArray basicSize bytes: byteArray
  200. !
  201.  
  202. putString: aString
  203.     aString do: [ :char | self byte: char asciiValue ]
  204. !
  205.  
  206. padBytes: len
  207.     ((4 - len) bitAnd: 3) timesRepeat: [ self byte: 0 ]
  208. !
  209.  
  210. wait: anInteger            "maybe a Delay at some point?, or a Time"
  211.     ^(self waitForSocket: socket timeOut: anInteger) = 1
  212. !!
  213.  
  214.  
  215. !X class methodsFor: 'foo'!    "this shouldn't be X class"
  216.  
  217. map: aSymbol into: anArray
  218.     ^(anArray indexOf: aSymbol 
  219.           ifAbsent: [ ^self error: 'Can''t map ', 
  220.                   aSymbol printString, ' into', 
  221.                   anArray printString])
  222.     - 1
  223. !
  224.  
  225. maybeMap: aSymbol into: anArray
  226.     ^(anArray indexOf: aSymbol 
  227.           ifAbsent: [ ^aSymbol id ])
  228.     - 1
  229. !
  230.  
  231. declareBitNames: bitArray inDictionary: aDict
  232.     | bit |
  233.     bit _ 1.
  234.     bitArray do:
  235.     [ :sym | sym notNil
  236.              ifTrue: [ aDict at: sym put: bit ].
  237.          bit _ bit bitShift: 1 ]
  238. !!
  239.  
  240.  
  241.  
  242. !X methodsFor: 'private'!
  243.  
  244. init: socketFD
  245.     socket _ socketFD
  246. !!
  247.  
  248.